﻿using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Data;

using XCode;
using XCode.Attributes;
using XCode.Entity;

namespace XCMSData
{
    /// <summary>
    /// 栏目
    /// </summary>
    public partial class Board : Entity
    {
        #region 事件
        #region 初始化
        /// <summary>
        /// 初始化 栏目 对象时触发
        /// </summary>
        /// <param name="k">初始化位置，1是空参数，2是全参数，3是标识参数</param>
        private void OnInit(int k)
        {
            if (!EnableCache) EnableCache = true;
        }
        #endregion

        #region 插入
        /// <summary>
        /// 插入前
        /// </summary>
        /// <returns>是否允许插入</returns>
        private static Boolean OnInserting()
        {
            return true;
        }

        /// <summary>
        /// 插入后
        /// </summary>
        /// <param name="retval">影响的行数</param>
        /// <returns>影响的行数</returns>
        private static Int32 OnInserted(Int32 retval)
        {
            hasUpdated = false;
            return retval;
        }

        /// <summary>
        /// 已重载。把该对象插入到数据库。
        /// </summary>
        /// <returns>影响的行数</returns>
        public override Int32 Insert()
        {
            if (!OnInserting()) return -1;
            return OnInserted(base.Insert());
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新前
        /// </summary>
        /// <returns>是否允许更新</returns>
        private static Boolean OnUpdating()
        {
            return true;
        }

        /// <summary>
        /// 更新后
        /// </summary>
        /// <param name="retval">影响的行数</param>
        /// <returns>影响的行数</returns>
        private static Int32 OnUpdated(Int32 retval)
        {
            hasUpdated = false;
            return retval;
        }

        /// <summary>
        /// 已重载。更新数据库。
        /// </summary>
        /// <returns>影响的行数</returns>
        public override Int32 Update()
        {
            if (!OnUpdating()) return -1;
            return OnUpdated(base.Update());
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除前
        /// </summary>
        /// <returns>是否允许删除</returns>
        private static Boolean OnDeleting()
        {
            return true;
        }

        /// <summary>
        /// 删除后
        /// </summary>
        /// <param name="retval">影响的行数</param>
        /// <returns>影响的行数</returns>
        private static Int32 OnDeleted(Int32 retval)
        {
            hasUpdated = false;
            return retval;
        }

        /// <summary>
        /// 已重载。从数据库中删除该对象
        /// </summary>
        /// <returns>影响的行数</returns>
        public override Int32 Delete()
        {
            if (!OnDeleting()) return -1;
            return OnDeleted(base.Delete());
        }
        #endregion
        #endregion

        #region 扩展属性
        /// <summary>
        /// 父栏目
        /// </summary>
        public Board Parent
        {
            get
            {
                return Board.FindByKey(this.ParentID);
            }
        }

        /// <summary>
        /// 父栏目
        /// </summary>
        [Description("父栏目")]
        [DataObjectField(false, false, true, 50)]
        [BindColumn("", Description = "父栏目")]
        public String ParentBoardName
        {
            get { return Parent == null ? "" : Parent.BoardName; }
        }
        #endregion

        #region 扩展方法
        /// <summary>
        /// 根据栏目编号，获取可作为其父栏目的所有栏目
        /// </summary>
        /// <param name="__ID">栏目编号</param>
        /// <returns>栏目 实体数组</returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static IList<Board> SelectAllForParent(Int32 __ID)
        {
            IList<Board> list = SelectAll();
            if (list == null || list.Count < 1) return null;

            if (__ID < 1) return list;
            //复制一份列表，否则待会删除的时候会删除原列表的，影响Cache
            IList<Board> ls = new List<Board>();
            Board board = null;
            foreach (Board b in list)
            {
                //自身不能作为自身的父栏目
                if (b.ID != __ID) ls.Add(b);
                else
                    board = b;
            }

            //自身不能作为儿子的父栏目
            ls = RemoveChildBoard(ls, board);
            return ls;
        }

        /// <summary>
        /// 从列表中排除指定栏目的子栏目，递归解决
        /// </summary>
        /// <param name="list"></param>
        /// <param name="board"></param>
        /// <returns></returns>
        private static IList<Board> RemoveChildBoard(IList<Board> list, Board board)
        {
            if (list == null || list.Count < 1) return null;
            if (board == null) return list;

            list.Remove(board);
            List<Board> todel = new List<Board>();
            foreach (Board b in list)
            {
                if (b.ParentID == board.ID) todel.Add(b);
            }
            foreach (Board b in todel)
            {
                list = RemoveChildBoard(list, b);
            }
            return list;
        }

        /// <summary>
        /// 首页栏目列表
        /// </summary>
        /// <returns></returns>
        public static IList<Board> SelectAllForIndex()
        {
            //找到首页栏目，因为首页栏目下的栏目才是需要在首页显示的。
            IList<Board> bs = Board.SelectAll(Board._.BoardName, "首页");
            //如果找不到首页，就取前面十个，如果找到，就取十个子栏目
            if (bs == null || bs.Count < 1)
                bs = Board.SelectAll(null, null, Board._.Order + " Desc", 0, 10);
            else
                bs = Board.SelectAll(Board._.ParentID + "=" + bs[0].ID, Board._.Order + " Desc", null, 0, 10);
            return bs;
        }
        #endregion
    }
}